package trabalho3.model;

import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import javax.imageio.ImageIO;

import model.org.jtrace.Materials;
import model.org.jtrace.Scene;
import model.org.jtrace.ViewPlane;
import model.org.jtrace.cameras.Camera;
import model.org.jtrace.cameras.PinHoleCamera;
import model.org.jtrace.geometry.Plane;
import model.org.jtrace.geometry.Triangle;
import model.org.jtrace.lights.Light;
import model.org.jtrace.primitives.ColorRGB;
import model.org.jtrace.primitives.Point3D;
import model.org.jtrace.primitives.Vector3D;
import action.org.jtrace.MultiThreadTracer;
import action.org.jtrace.Tracer;
import action.org.jtrace.interceptor.ShadowInterceptor;
import action.org.jtrace.io.PlyReader;
import action.org.jtrace.listeners.ImageListener;
import action.org.jtrace.listeners.TimeListener;
import action.org.jtrace.shader.Shaders;


public class BeethovenPLYExample {
	public static void main(String[] args) throws IOException, InterruptedException {
		//InputStream is = SimplePLYExample.class.getResourceAsStream("sphere.ply");
		InputStream is = new FileInputStream(new File("ply/sphere.ply"));
		List<Triangle> triangles;
 
		triangles = new PlyReader().read(is, Materials.matte(ColorRGB.PURPLE));
		Scene scene = new Scene();
		
		BufferedImage image = null;
		image = ImageIO.read(new File("textures/strawberry.jpg"));
		int width = image.getWidth();
		int height = image.getHeight();
		
		ImageTexture imageTexture = new ImageTexture(image, width, height);
		
		for (Triangle t : triangles) {
			t.getMaterial().setImageTexture(imageTexture);
			scene.add(t);
		}
		
		//scene.add(new Light(140, 200, 140));
		scene.add(new Light(140, 140, 140));
		
		Camera camera = new PinHoleCamera(new Point3D(140, 140, 140), new Point3D(139, 139, 139), Vector3D.UNIT_Z);
		
		camera.setZoomFactor(50);
		
		scene.setCamera(camera);
		final Plane lowerPlane = new Plane(new Point3D(0, -140.1, -15), new Vector3D(0, 0, 1), Materials.metallic(ColorRGB.YELLOW));
		
		//scene.add(lowerPlane);
		
		//Tracer tracer = new Tracer();
		Tracer tracer = new MultiThreadTracer(10);
		
		tracer.addInterceptors(new ShadowInterceptor());
		tracer.addShaders(Shaders.ambientShader(), Shaders.diffuseShader(), Shaders.specularShader(64));
		
		tracer.addListeners(new ImageListener("teste5.png", "png"), new TimeListener());
		
		tracer.render(scene, new ViewPlane(1280, 768));
		
		Toolkit.getDefaultToolkit().beep();
	}

}
